﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using SpaceEngine.Assets;
using SpaceEngine.Input;

namespace SpaceEngine
{

    public class Game1 : Microsoft.Xna.Framework.Game
    {
        public PlayerShip ship;
        Map map;
        Starfield starfield;
        Station testStation;
        Texture2D background;
        
        public SpriteBatch spriteBatch;
        public ContentManager manager;
        public GraphicsDeviceManager graphics;
        public Camera camera;
        public Cursor cursor;
        public InputWin input;
        public Random rand;
        public enum GameState { Splash, MainMenu, LoadNew, LoadSaved, InGame, InGamePause, InGameMenu, EndGame, Exit };
        public GameState currentState;

        public Game1()
        {
            Window.Title = "Space RPG";
            manager = new ContentManager(Services);
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferWidth = 800;
            graphics.PreferredBackBufferHeight = 600;
            graphics.IsFullScreen = false;
            rand = new Random();
            Content.RootDirectory = "Content";
            currentState = GameState.Splash;
        }

        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
        }

        protected override void UnloadContent()
        {

        }
       
        protected override void Update(GameTime gameTime)
        {

            switch (currentState)
            {
                case GameState.Splash : // startup splash sequence
                    // no splash yet, move on
                    currentState = GameState.MainMenu;
                    break;
                case GameState.MainMenu : // main game menu
                    // no menu yet, move on
                    currentState = GameState.LoadNew;
                    break;
                case GameState.LoadNew: // create a new game
                    // load and play!
                    LoadGame();
                    currentState = GameState.InGame;
                    break;
                case GameState.LoadSaved: // load an existing game from file
                    break;
                case GameState.InGame : // game in progress
                    // update mouse, camera and map
                    camera.Update();
                    map.Update();
                    break;
                case GameState.InGameMenu : // game in progress but paused for menu use
                    break;
                case GameState.InGamePause : // game in progress, generic pause
                    break;
                case GameState.EndGame : // game is over, unload, show scores
                    break;
                case GameState.Exit : // kill the game
                    Exit();
                    break;
            }
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.DimGray);

            switch (currentState)
            {
                case GameState.Splash: // startup splash sequence
                    break;

                case GameState.MainMenu: // main game menu
                    break;

                case GameState.LoadNew: // create a new game
                    break;

                case GameState.LoadSaved: // load an existing game from file
                    break;

                case GameState.InGame: // game in progress
                    DrawBackground();
                    break;

                case GameState.InGameMenu: // game in progress but paused for menu use
                    break;

                case GameState.InGamePause: // game in progress, generic pause
                    break;

                case GameState.EndGame: // game is over, unload, show scores
                    break;
            }
            
            base.Draw(gameTime);
        }


        private void LoadGame()
        {
            // load background starfield
            background = manager.Load<Texture2D>(@"Content\Textures\background_001");

            // init components
            camera = new Camera(this);
            input = new InputWin(this);
            map = new Map(this);
            ship = new PlayerShip(this);
            cursor = new Cursor(this);
            starfield = new Starfield(this, 1000);
            testStation = new Station(this, new Vector3(0, 20, 0));

            // add components
            Components.Add(input);
            Components.Add(starfield);
            Components.Add(ship);
            Components.Add(testStation);
            Components.Add(cursor);
        }

        private void DrawBackground()
        {
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.BackToFront, SaveStateMode.None);
            spriteBatch.Draw(background, Vector2.Zero, Color.White);
            spriteBatch.End();
            graphics.GraphicsDevice.RenderState.DepthBufferEnable = true;
        }

        public void ExitGame()
        {
            // move thru unload phase, save data, etc.

            currentState = GameState.Exit;
        }

    }
}
